//
//  NationClass.m
//  SHHCPhone
//
//  Created by Sheng long on 14-8-21.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "NationClass.h"

@implementation NationClass

//获取民族数据
+(void)saveNationData
{
    CLog(@"获取民族数据");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *text = [APP_USERDEFAULTS valueForKey:@"hqmzcg_nation"];
        if (!text) {
            
            NSDictionary *dic = @{IOS_METHOD_NAME: IOS_NATION};
            ASIHTTPRequest *request =  [z_Network httpGet:dic Url:IOS_WEBURL Delegate:nil Tag:IOS_NATION_TAG];
            [request startSynchronous];
            
            NSError *error = [request error];
            
            //网络状态正常
            if (!error){
                //获取服务器返回值
                id data = [[request responseString] objectFromJSONString];
                if ([data isKindOfClass:[NSArray class]]) {
                    //
                    CLog(@"保存民族成功");
                    //保存民族信息
                    [APP_USERDEFAULTS setValue:[request responseString] forKey:@"hqmzcg_nation"];
                }
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    });
}

//获取民族数据
+(NSArray*)getNationData
{
    NSString *text = [APP_USERDEFAULTS valueForKey:@"hqmzcg_nation"];
    if (text) {
        return [text objectFromJSONString];
    }
    
    return nil;
}

//传入id获取所属位置
+(NSInteger)getIDLoc:(NSString*)ID
{
    NSArray *array = [self getNationData];
    for (NSDictionary *dic in array) {
        if ([ID isEqualToString:[dic valueForKeyPath:@"id"]]) {
            return [array indexOfObject:dic];
        }
    }
    return 0;
}

//传入id获取所属民族名称
+(NSString*)getIDName:(NSString*)ID
{
    NSArray *array = [self getNationData];
    for (NSDictionary *dic in array) {
        if ([ID isEqualToString:[dic valueForKeyPath:@"id"]]) {
            return [dic valueForKeyPath:@"nation"];
        }
    }
    return nil;
}

//传入名称获取所属位置
+(NSInteger)getNameLoc:(NSString*)name
{
    NSArray *array = [self getNationData];
    for (NSDictionary *dic in array) {
        if ([name isEqualToString:[dic valueForKeyPath:@"nation"]]) {
            return [array indexOfObject:dic];
        }
    }
    return 0;
}

//传入名称获取所属id
+(NSString*)getNameID:(NSString*)name
{
    NSArray *array = [self getNationData];
    for (NSDictionary *dic in array) {
        if ([name isEqualToString:[dic valueForKeyPath:@"nation"]]) {
            return [dic valueForKeyPath:@"id"];
        }
    }
    return nil;
}

@end
